package cn.cmvideo.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.cmvideo.springboot.dao.UserDao;
import cn.cmvideo.springboot.entity.User;

/**
 * 注意：@RestController注解相当于@ResponseBody ＋ @Controller合在一起的作用。<br>
 * 1.如果只是使用RestController 注解 Controller，则Controller中的方法无法调用JSP页面或者HTML，
 * 配置的视图解析器InternalResourceViewResolver不起作用，返回的内容就是return中的内容<br>
 * 2.如果需要返回指定页面，则需要@Controller配合视图解析器InternalResourceViewResolver配合使用<br>
 * 3.如果需要返回JSON、XML或自定义MediaType内容到页面，则需要在对应的方法上加上@RequestBody(@Controller)
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(path = "/demo")
public class UserController
{
	@Autowired
	private UserDao userDao;

	@GetMapping(path = "/save")
	public @ResponseBody String saveUser(@RequestParam String name, @RequestParam String email)
	{
		User user = new User();
		user.setName(name);
		user.setEmail(email);
		userDao.save(user);
		return "saved!";
	}

	@RequestMapping(path = "/all", method = RequestMethod.GET)
	public @ResponseBody Iterable<User> getAllUsers()
	{
		Iterable<User> findAll = userDao.findAll();
		return findAll;
	}

}
